
#import "ViewController.h"
#import <Masonry/Masonry.h>


#import "ZSDragView.h"

@interface ViewController ()

@property (nonatomic, strong) ZSDragView *dragView;

@property (assign, nonatomic) CGPoint originalPoint;

@property (assign, nonatomic) CGFloat moveY;
@property (assign, nonatomic) CGFloat moveX;

@end

#define KWidth  [[UIScreen mainScreen] bounds].size.width
#define WeakSelf    __weak typeof(self) weakSelf = self;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.dragView];
    _moveY = -50;
    [_dragView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.view.mas_centerY).offset(_moveY);
        make.centerX.mas_equalTo(self.view.mas_centerX).offset(_moveX);
        make.width.mas_equalTo(KWidth - 104);
        make.height.mas_greaterThanOrEqualTo(120);
    }];
    
    NSString *alertMessage = @"我是一个支持手势移动的控件\n支持 Masonry frame 布局 \n\n使用富文本\n\n也支持自动高度\n核心就是记录你每次滑动的距离更新中心点";
    self.dragView.tagLab.text = alertMessage;
    
    NSMutableAttributedString * mutAtt = [[NSMutableAttributedString alloc]initWithString:alertMessage];
    NSRange ttzsRang = NSMakeRange([[mutAtt string] rangeOfString:@"使用富文本"].location, [[mutAtt string] rangeOfString:@"使用富文本"].length);
    [mutAtt addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:ttzsRang];
    [mutAtt addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:18] range:ttzsRang];
    
    //组简码
    NSRange bgdRang = NSMakeRange([[mutAtt string] rangeOfString:@"也支持自动高度"].location, [[mutAtt string] rangeOfString:@"也支持自动高度"].length);
    [mutAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:bgdRang];

    self.dragView.tagLab.attributedText = mutAtt;
}

- (void)handlePanGesture:(UIPanGestureRecognizer *)p {
    CGPoint panPoint = [p locationInView:self.view];
    CGPoint currentViewPoint = _dragView.center;

    if (p.state == UIGestureRecognizerStateBegan) {
        _originalPoint = panPoint;
    } else if(p.state == UIGestureRecognizerStateChanged) {


        CGFloat moveCenterY = panPoint.y - _originalPoint.y;
        CGFloat moveCenterX = panPoint.x - _originalPoint.x;
        
        _moveY += moveCenterY;
        _moveX += moveCenterX;
        
        [self.dragView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.view.mas_centerY).offset(_moveY);
            make.centerX.mas_equalTo(self.view.mas_centerX).offset(_moveX);
        }];
        //使用 frame 的话 这样布局
        //self.forecastGroupView.center = CGPointMake(currentViewPoint.x + moveCenterX, currentViewPoint.y + moveCenterY);
        _originalPoint = panPoint;

    } else if (p.state == UIGestureRecognizerStateEnded
             || p.state == UIGestureRecognizerStateCancelled) {
        
        CGFloat groupViewHeight = _dragView.frame.size.height;
        CGFloat groupViewWidth = _dragView.frame.size.width;
        CGFloat parentViewHeight = self.view.frame.size.height;
        CGFloat parentViewWidth = self.view.frame.size.width;
        

        CGFloat centerY = 0.0;
        BOOL edit = NO;
        
        if (currentViewPoint.y < groupViewHeight/2) {
            centerY = groupViewHeight/2;
            _moveY = -(parentViewHeight - groupViewHeight)/2;
            edit = YES;
        } else if (currentViewPoint.y > parentViewHeight - groupViewHeight/2) {
            _moveY = centerY;
            _moveY = (parentViewHeight - groupViewHeight)/2;
            edit = YES;
        } else {
            centerY = currentViewPoint.y;
        }

        
        CGFloat centerX = 0.0;
        if (currentViewPoint.x < groupViewWidth/2) {
            centerX = groupViewWidth/2;
            _moveX = -(KWidth - groupViewWidth)/2;
            edit = YES;
        } else if (currentViewPoint.x > parentViewWidth - groupViewWidth/2) {
            centerX = parentViewWidth - groupViewWidth/2;
            _moveX = (KWidth - groupViewWidth)/2;
            edit = YES;
        } else {
            centerX = currentViewPoint.x;
        }

        if(edit) {
            WeakSelf
            [UIView animateWithDuration:.25 animations:^{
                //使用 frame 的话 这样布局
                //self.forecastGroupView.center = CGPointMake(centerX, centerY);
                [weakSelf.dragView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(self.view.mas_centerY).offset(self->_moveY);
                    make.centerX.mas_equalTo(self.view.mas_centerX).offset(self->_moveX);
                }];
                
            } completion:^(BOOL finished) {}];
        }
        
    } else {
        NSLog(@" pan state : %zd", p.state);
    }
}


- (ZSDragView *)dragView {
    if(!_dragView) {
        _dragView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZSDragView class]) owner:nil options:nil].firstObject;
        _dragView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        _dragView.hidden = YES;
        _dragView.layer.masksToBounds = YES;
        _dragView.layer.cornerRadius = 4;
        _dragView.userInteractionEnabled = YES;
        /*
        UITapGestureRecognizer *shortTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shortClick)];
        [_forecastGroupView addGestureRecognizer:shortTap];
         */
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
        [_dragView addGestureRecognizer:pan];
    }
    return _dragView;
}

@end
